from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship

from db.model.base import Base


class Device(Base):
    __tablename__ = "device"

    id = Column(Integer, primary_key=True, comment="id")
    name = Column(String(32), nullable=False, unique=True, comment="设备名称")
    ip = Column(String(32), nullable=False, comment="Ip地址")
    port = Column(Integer, nullable=False, comment="端口")
    protocol = Column(String(32), nullable=False, comment="通讯类型")

    # 关联从站
    slaves = relationship("Slave", back_populates="device")

    __table_args__ = (UniqueConstraint("ip", "port", name="uq_connection"), {"comment": "设备表"})
